﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _Nuttaps2
    {
        public static void Nuttaps2(Dictionary<string, Object> data)//螺母丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
            //作图
            //参数
            double pi = Math.PI;
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            string path = Convert.ToString(data["path"]);
            double td = Convert.ToDouble(data["td"]) * scale;
            double tp = Convert.ToDouble(data["tp"]) * scale;
            double oal = Convert.ToDouble(data["oal"]) * scale;
            double thl = Convert.ToDouble(data["thl"]) * scale;
            double tcl = Convert.ToDouble(data["lu"]) * scale;
            double dconms = Convert.ToDouble(data["dconms"]) * scale;
            double drvsc = Convert.ToDouble(data["drvsc"]) * scale;
            double drvl = Convert.ToDouble(data["drvl"]) * scale;
            double l5 = Convert.ToDouble(data["tcl"])*tp * scale;

            /*double td = 0.8;
            double tp = 0.2;
            double oal = 30;
            double thl = 5;
            double tcl = 6;
            double dconms = 3;
            double drvsc = 2.5;
            double drvl = 5;*/


            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + oal + 5, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 2.5, v1.Y);
            Vector2 v5 = new Vector2(v3.X + l5, v3.Y + td / 2);
            Vector2 v4 = new Vector2(v3.X, v3.Y + (td / 2) * 3 / 4);
            Vector2 v6 = new Vector2(v3.X + tcl, v5.Y);
            Vector2 v7 = new Vector2(v6.X + (oal - tcl) / 5, v3.Y + dconms / 2);
            Vector2 v8 = new Vector2(v3.X + oal, v7.Y);
            Vector2 a3 = new Vector2(v3.X, v3.Y * -1);
            Vector2 a4 = new Vector2(v4.X, v4.Y * -1);
            Vector2 a5 = new Vector2(v5.X, v5.Y * -1);
            Vector2 a6 = new Vector2(v6.X, v6.Y * -1);
            Vector2 a7 = new Vector2(v7.X, v7.Y * -1);
            Vector2 a8 = new Vector2(v8.X, v8.Y * -1);
            Vector2 v9 = new Vector2(v3.X, v4.Y-(v4.Y-v3.Y ) /6);
            Vector2 v10 = new Vector2(v5.X, v5.Y - (v4.Y - v9.Y));
            Vector2 v11 = new Vector2(v3.X + thl, v10.Y);
            Vector2 v12 = new Vector2(v11.X - (v4.Y - v9.Y), v10.Y);
            Vector2 a9 = new Vector2(v9.X, v9.Y * -1);
            Vector2 a10 = new Vector2(v10.X, v10.Y * -1);
            Vector2 a11 = new Vector2(v11.X, v11.Y * -1);
            Vector2 a12 = new Vector2(v12.X, v12.Y * -1);
            
            //方头
            Vector2 v18 = fun.Polar(v3, pi, dconms / 2);
            Vector2 v19 = fun.Polar(new Vector2(v8.X, v1.Y), 0, dconms / 2);
            Vector2 v20 = fun.Polar(v8, -90 * pi / 180, dconms / 3);
            Vector2 v21 = fun.Polar(v20, pi, drvl);
            Vector2 v22 = new Vector2(v21.X, v8.Y);
            Vector2 a20 = new Vector2(v20.X, -v20.Y);
            Vector2 a21 = new Vector2(v21.X, -v21.Y);
            Vector2 a22 = new Vector2(v22.X, -v22.Y);


            Vector2 v019 = fun.Polar(v19, 0, drvsc * 3);
            Vector2 v020 = fun.Polar(v019, 90 * pi / 180, drvsc);
            Vector2 v021 = fun.Polar(v019, 0, drvsc);
            Vector2 v022 = fun.Polar(v019, pi, drvsc);
            Vector2 v023 = fun.Polar(v019, -90 * pi / 180, drvsc);
            Vector2 v020p = fun.Polar(v019, 90 * pi / 180, drvsc * Math.Sin(45 * pi / 180));
            Vector2 v021p = fun.Polar(v019, 0, drvsc * Math.Sin(45 * pi / 180));
            Vector2 v022p = fun.Polar(v019, pi, drvsc * Math.Sin(45 * pi / 180));
            Vector2 v023p = fun.Polar(v019, -90 * pi / 180, drvsc * Math.Sin(45 * pi / 180));
            Vector2 v024 = fun.Polar(v020p, -135 * pi / 180, drvsc / 5);
            Vector2 v025 = fun.Polar(v020p, -45 * pi / 180, drvsc / 5);
            Vector2 v026 = fun.Polar(v021p, 135 * pi / 180, drvsc / 5);
            Vector2 v027 = fun.Polar(v021p, -135 * pi / 180, drvsc / 5);
            Vector2 v028 = fun.Polar(v023p, 45 * pi / 180, drvsc / 5);
            Vector2 v029 = fun.Polar(v023p, 135 * pi / 180, drvsc / 5);
            Vector2 v030 = fun.Polar(v022p, -45 * pi / 180, drvsc / 5);
            Vector2 v031 = fun.Polar(v022p, 45 * pi / 180, drvsc / 5);



            LwPolyline poly0 = new LwPolyline();
            poly0.Vertexes.Add(new LwPolylineVertex(v4));
            poly0.Vertexes.Add(new LwPolylineVertex(v5));
            poly0.Vertexes.Add(new LwPolylineVertex(v6));
            poly0.Vertexes.Add(new LwPolylineVertex(v7));
            poly0.Vertexes.Add(new LwPolylineVertex(v8));
            poly0.Vertexes.Add(new LwPolylineVertex(a8));
            poly0.Vertexes.Add(new LwPolylineVertex(a7));
            poly0.Vertexes.Add(new LwPolylineVertex(a6));
            poly0.Vertexes.Add(new LwPolylineVertex(a5));
            poly0.Vertexes.Add(new LwPolylineVertex(a4));
            poly0.Vertexes.Add(new LwPolylineVertex(v4));
            fun.PolyFill(dxf, poly0, 0.1, contour);

            LwPolyline poly1 = new LwPolyline();
            poly1.Vertexes.Add(new LwPolylineVertex(v5));
            poly1.Vertexes.Add(new LwPolylineVertex(a5));
            poly1.Vertexes.Add(new LwPolylineVertex(a6));
            poly1.Vertexes.Add(new LwPolylineVertex(v6));
            poly1.Vertexes.Add(new LwPolylineVertex(v7));
            poly1.Vertexes.Add(new LwPolylineVertex(a7));
            fun.PolyFill(dxf, poly1, 0.1, contour);

            LwPolyline poly2 = new LwPolyline();
            poly2.Vertexes.Add(new LwPolylineVertex(v9));
            poly2.Vertexes.Add(new LwPolylineVertex(v10));
            poly2.Vertexes.Add(new LwPolylineVertex(v12));
            poly2.Vertexes.Add(new LwPolylineVertex(v11));
            poly2.Vertexes.Add(new LwPolylineVertex(v12));
            poly2.Vertexes.Add(new LwPolylineVertex(a12));
            poly2.Vertexes.Add(new LwPolylineVertex(a11));
            poly2.Vertexes.Add(new LwPolylineVertex(a12));
            poly2.Vertexes.Add(new LwPolylineVertex(a10));
            poly2.Vertexes.Add(new LwPolylineVertex(a9));
            fun.PolyFill(dxf, poly2, 0.05, contour);





            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 3.5;
            dimensionStyle.ExtLineExtend = 0.6;//标注延长线长度/2
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            //文本样式
            dimensionStyle.TextHeight = 1;
            dimensionStyle.TextOffset = 0.25;
            double offset = 5;



            Vector2[] Line9 = { v8, a8 };
            fun.drawBLine(dxf, Line9, 0.1, contour);
            Vector2[] Line10 = { v20, v21 };
            fun.drawBLine(dxf, Line10, 0.1, contour);
            Vector2[] Line11 = { v21, v22 };
            fun.drawBLine(dxf, Line11, 0.1, contour);
            Vector2[] Line12 = { a20, a21 };
            fun.drawBLine(dxf, Line12, 0.1, contour);
            Vector2[] Line13 = { a21, a22 };
            fun.drawBLine(dxf, Line13, 0.1, contour);
            Vector2[] Line14 = { v19, v18 };
            fun.drawPlinetype(dxf, Line14, 0.05, centre, Linetype.Center);
            Vector2[] Line15 = { v020, v023 };
            fun.drawPlinetype(dxf, Line15, 0.05, centre, Linetype.Center);
            Vector2[] Line16 = { v022, v021 };
            fun.drawPlinetype(dxf, Line16, 0.05, centre, Linetype.Center);

            LwPolyline poly8 = new LwPolyline();
            poly8.Vertexes.Add(new LwPolylineVertex(v031));
            poly8.Vertexes.Add(new LwPolylineVertex(v024));
            poly8.Vertexes.Add(new LwPolylineVertex(v025));
            poly8.Vertexes[1].Bulge = -Math.Tan(pi / 8);
            poly8.Vertexes.Add(new LwPolylineVertex(v026));
            poly8.Vertexes[3].Bulge = -Math.Tan(pi / 8);
            poly8.Vertexes.Add(new LwPolylineVertex(v027));
            poly8.Vertexes.Add(new LwPolylineVertex(v028));
            poly8.Vertexes[5].Bulge = -Math.Tan(pi / 8);
            poly8.Vertexes.Add(new LwPolylineVertex(v029));
            poly8.Vertexes.Add(new LwPolylineVertex(v030));
            poly8.Vertexes[7].Bulge = -Math.Tan(pi / 8);
            poly8.IsClosed = true;
            fun.PolyFill(dxf, poly8, 0.3, contour);

            LwPolyline poly9 = new LwPolyline();
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = poly8.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = poly8.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)poly8.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)poly8.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 5, hline1, harc1, hatching);

            LinearDimension dim1 = new LinearDimension(new Line(v5, a5), offset+ l5, 90, dimensionStyle);
            dim1.UserText = "TD = " + td/ scale;
            dxf.AddEntity(dim1);

            LinearDimension dim2 = new LinearDimension(new Line(a4, a11), offset, 180, dimensionStyle);
            dim2.UserText = "THL = " + thl / scale;
            dxf.AddEntity(dim2);

            LinearDimension dim3 = new LinearDimension(new Line(a4, a6), offset*2, 180, dimensionStyle);
            dim3.UserText = "LU = " + tcl / scale;
            dxf.AddEntity(dim3);

            LinearDimension dim4 = new LinearDimension(new Line(a4, a8), offset * 3, 180, dimensionStyle);
            dim4.UserText = "OAL = " + oal / scale;
            dxf.AddEntity(dim4);

            LinearDimension dim5 = new LinearDimension(new Line(a7, v7), offset * 2, -90, dimensionStyle);
            dim5.UserText = "DCONMS = " + dconms / scale;
            dxf.AddEntity(dim5);

            LinearDimension dim10 = new LinearDimension(new Line(v029, v026), offset+dconms/2, -135, dimensionStyle);
            dim10.UserText = "DRVSC = " + drvsc / scale;
            dxf.AddEntity(dim10);

            LinearDimension dim11 = new LinearDimension(new Line(a22, new Vector2 (v8.X ,a22.Y )), offset, 180, dimensionStyle);
            dim11.UserText = "DRVL = " + drvl / scale;
            dxf.AddEntity(dim11);

            LinearDimension dim12 = new LinearDimension(new Line(v4, v5), offset, 0, dimensionStyle);
            dim12.UserText = "TCL = " + l5 / scale;
            dxf.AddEntity(dim12);



            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - offset * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }


            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
}
}